local zero = {
	__index = function (t, v) return 0 end,
}

local function GetCombatRating(value, base, level)
	local m =  (3.1951219512195124 - 0.036585365853658534 * level)
	return value / base * m
end


local items = {
	["Stormrage Cover"] = {
		slots = { head = true },
		stats = {
			armor = 208,
			stamina = 20,
			intellect = 31,
			spirit = 12,
			frost = 10,
			shadow = 10,
			regen = 6,
			heal = 29,
		},
	},
	["Forest Wind Shoulderpads"] = {
		slots = { shoulder = true, },
		stats = {
			armor = 239,
			stamina = 16,
			intellect = 25,
			spirit = 23,
			heal = 59,
		}
	},
	["Stormrage Chestguard"] = {
		slots = { chest = true, },
		stats = {
			armor = 257,
			stamina = 20,
			intellect = 25,
			spirit = 17,
			fire = 10,
			nature = 10,
--~ 			spellcrit = 14,
			spellcrit = GetCombatRating(14, 14, 70),
			heal = 42,
		},
	},
	["Stormrage Legguards"] = {
		slots = { legs = true, },
		stats = {
			armor = 224,
			stamina = 17,
			intellect = 26,
			spirit = 16,
			arcane = 10,
			fire = 10,
			heal = 48,
			regen = 6,
		},
	},
	["Stormrage Belt"] = {
		slots = { waist = true, },
		stats = {
			armor = 144,
			stamina = 12,
			intellect = 23,
			spirit = 10,
			shadow = 10,
			heal = 26,
			regen = 4,
		},
	},
	["Stormrage Bracers"] = {
		slots = { wrist = true, },
		stats = {
			armor = 112,
			stamina = 11,
			intellect = 15,
			spirit = 12,
			heal = 33,
		},
	},
	["Mitts of the Treemender"] = {
		slots = { hands = true, },
		stats = {
			armor = 199,
			stamina = 22,
			intellect = 19,
			spirit = 12,
			heal = 57,
		},
		sockets = {
			[1] = "blue",
			[2] = "yellow",
			bonus = {
				spirit = 3,
			},
		},
	},
	["Forestlord Striders"] = {
		slots = { feet = true, },
		stats = {
			armor = 219,
			stamina = 21,
			intellect = 24,
			spirit = 14,
			heal = 48,
		},
		sockets = {
			[1] = "blue",
			[2] = "blue",
			bonus = {
				heal = 7,
			}
		},
	},
	["Serpentcrest Life-staff"] = {
		slots = { mainhand = true, },
		stats = {
			stamina = 27,
			intellect = 27,
			spirit = 46,
			heal = 227,
		},
	},
	["Cloak of Whispering Shells"] = {
		slots = { back = true, },
		stats = {
			stamina = 15,
			intellect = 16,
			regen = 4,
			heal = 40,
		},
	},
	["Violet Signet"] = {
		slots = { finger1 = true, finger2 = true, },
		stats = {
			stamina = 18,
			intellect = 19,
			spirit = 13,
			heal = 44,
		},
	},
	["Pure Elementium Band"] = {
		slots = { finger1 = true, finger2 = true, },
		stats = {
			stamina = 9,
			intellect = 10,
			spirit = 10,
			heal = 53,
		},
	},
	["Rejuvenating Gem"] = {
		slots = { trinket1 = true, trinker2 = true, },
		stats = {
			heal = 66,
			regen = 9,
		},
	},
	["Heavenly Inspiration"] = {
		slots = { trinket1 = true, trinket2 = true, },
		stats = {
			regen = 10,
		},
	},
	["Sethekk Oracle's Focus"] = {
		slots = { neck = true },
		stats = {
			intellect = 19,
			heal = 35,
			regen = 8,
		}
	},
	-- other stuff
	["Swampstone Necklace"] = { -- heroic underbog
		slots = { neck = true },
		stats = {
			stamina = 14,
			intellect = 15,
			spirit = 18,
			heal = 40,
		},
	},
	["Crown of the Forest Lord"] = { -- Heroic underbog
		slots = { head = true },
		stats = {
			armor = 237,
			stamina = 26,
			intellect = 30,
			spirit = 27,
			heal = 68,
		}
	},
	["Mantle of Autumn"] = { -- Botanica
		slots = { shoulder = true },
		stats = {
			armor = 219,
			stamina = 24,
			intellect = 31,
			spirit = 27,
			heal = 68,
		}
	},
	["Raiments of Nature's Breath"] = { -- Heroic remparts
		slots = { chest = true },
		stats = {
			armor = 292,
			stamina = 25,
			intellect = 32,
			spirit = 18,
			regen = 7,
			heal = 75,
		},
	},
	["The Sleeper's Cord"] = { -- Arcatraz
		slots = { waist = true },
		stats = {
			armor = 164,
			stamina = 18,
			intellect = 24,
			spirit = 21,
			heal = 53,
		},
	},
	["Earthsoul Britches"] = { -- heroic Slave pen 
		slots = { legs = true },
		stats = {
			armor = 256,
			stamina = 24,
			intellect = 33,
			spirit = 26,
			heal = 73,
		},
	},
	["Boots of the Glade-Keeper"] = { -- Mechanar
		slots = { feet = true },
		stats = {
			armor = 201,
			stamina = 21,
			intellect = 24,
			spirit = 20,
			heal = 53,
		},
	},
	["Lucid Dream Bracers"] = { -- heroic Mana Tombs
		slots = { wrist = true },
		stats = {
			armor = 128,
			stamina = 13,
			intellect = 15,
			spirit = 17,
			heal = 37,
		},
	},
	["Natural Mender's Wraps"] = { -- heroic Auchenai Crypts
		slots = { hands = true },
		stats = {
			armor = 183,
			stamina = 22,
			intellect = 25,
			spirit = 18,
			heal = 55,
		},
	},
--[[
	[""] = {
		slots = {},
		stats = {
		},
	},
	[""] = {
		slots = {},
		stats = {
		},
	},
	[""] = {
		slots = {},
		stats = {
		},
	},
	[""] = {
		slots = {},
		stats = {
		},
	},
	[""] = {
		slots = {},
		stats = {
		},
	},
	[""] = {
		slots = {},
		stats = {
		},
	},
	[""] = {
		slots = {},
		stats = {
		},
	},
	[""] = {
		slots = {},
		stats = {
		},
	},
	[""] = {
		slots = {},
		stats = {
		},
	},
]]
}

local enchants = {
	["Animist's Caress"] = {
		slots = { head = true, legs = true },
		stats = {
			stamina = 10,
			intellect = 10,
			heal = 24,
		},
	},
	["Inscription of Faith"] = {
		slots = { shoulder = true },
		stats = {
			heal = 29,
		},
	},
	["Enchant Chest - Restore Mana Prime"] = {
		slots = { chest = true },
		stats = {
			regen = 6,
		},
	},
	["Enchant Bracer - Mana Regeneration"] = {
		slots = { wrist = true },
		stats = {
			regen = 4,
		},
	},
	["Enchant Bracer - Restore Mana Prime"] = {
		slots = { wrist = true },
		stats = {
			regen = 6,
		},
	},
	["Enchant Gloves - Healing Power"] = {
		slots = { hands = true },
		stats = {
			heal = 30,
		},
	},
	["Enchant Gloves - Major Healing"] = {
		slots = { hands = true },
		stats = {
			heal = 35,
		},
	},
	["Enchant Boots - Spirit"] = {
		slots = { feet = true },
		stats = {
			spirit = 5,
		},
	},
	["Enchant Weapon - Mighty Spirit"] = {
		slots = { mainhand = true },
		stats = {
			spirit = 20,
		},
	},
	["Golden Spellthread"] = {
		slots = { legs = true },
		stats = {
			heal = 51,
			stamina = 18,
		},
	},
	["Silver Spellthread"] = {
		slots = { legs = true },
		stats = {
			heal = 46,
			stamina = 15,
		},
	},
	["Greater Inscription of Faith"] = {
		slots = { shoulder = true, },
		stats = {
			heal = 33,
			regen = 4,
		},
	},
	["Glyph of Renewal"] = {
		slots = { head = true, },
		stats = {
			heal = 35,
			regen = 7,
		},
	},
	["Enchant Chest - Major Spirit"] = {
		slots = { chest = true },
		stats = {
			spirit = 15,
		},
	},
}

local gems = {
	["Lustrous Star of Elune"] = {
		stats = {
			regen = 3,
		},
		colors = {
			blue = true,
		},
	},
	["Sparkling Azure Moonstone"] = {
		stats = {
			spirit = 6,
		},
		colors = {
			blue = true,
		}
	},
	["Luminous Noble Topaz"] = {
		stats = {
			heal = 9,
			intellect = 4
		},
		colors = {
			red = true,
			yellow = true,
		},
	},
}

local sets = {
	["Stormrage Raiment"] = {
		bonus = {
			[3] = { regen = 20, },
			[5] = { regcast = .2, },
			[8] = { rejuvlen = 3 },
		},
		pieces = {
			["Stormrage Belt"] = true,
			["Stormage Boots"] = true,
			["Stormage Bracers"] = true,
			["Stormrage Chestguard"] = true,
			["Stormrage Cover"] = true,
			["Stormrage Handguards"] = true,
			["Stormrage Legguards"] = true,
			["Stormrage Pauldrons"] = true,
		}
	}
}

do
	local putnames = function (t)
		for n, o in pairs(t) do
			o.name = n
		end
	end
	for _, t in ipairs({items, enchants, gems, sets}) do
		putnames(t)
	end
end


local function get_secondary_stats(stat)
	stat.total_mana =  stat.mana + stat.intellect * 15
	stat.intellect_spellcrit = stat.intellect / 60
	stat.total_spellcrit = stat.spellcrit + stat.intellect_spellcrit

	stat.total_health = stat.health + stat.stamina * 10

	stat.spirit_regen = stat.spirit / 4.5 + 15
	stat.casting_regen = stat.regen + stat.spirit_regen * .15
	stat.standy_regen = stat.regen + stat.spirit_regen
	stat.tree_heal_bonus = stat.spirit * .25
	
end

local function get_equipment_bonuses(equipment, stats)
	stats = stats or {}
	setmetatable(stats, zero)
	local tsets = setmetatable({}, zero)
	local gem_colors = setmetatable({}, zero)
	local gem_metas = {}
	for slot, info in pairs(equipment) do
		local item_name = info.item
		if item_name then
			local item = items[item_name]
			assert(item.slots[slot], string.format("Invalid Item in slot \"%s\"", slot))
			for name, value in pairs(item.stats) do
				stats[name] = stats[name] + value
			end
			local enchant_name = info.enchant
			if enchant_name then
				local enchant = enchants[enchant_name]
				assert(enchant.slots[slot], string.format("Invalid Enchantment in slot \"%s\"", slot))
				for name, value in pairs(enchant.stats) do
					stats[name] = stats[name] + value
				end
			end
			local sockets = item.sockets
			if sockets then
				local has_bonus = true
				for i, color in ipairs(sockets) do
					local gem_name = info["socket"..i]
					if not gem_name then
						has_bonus = false
					else
						local gem = gems[gem_name]
						for name, value in pairs(gem.stats) do
							stats[name] = stats[name] + value
						end
						if not gem.colors[color] then
							has_bonus = false
						end
						for color in pairs(gem.colors) do
							gem_colors[color] = gem_colors[color] + 1
						end
					end
				end
				if has_bonus then
					for name, value in pairs(sockets.bonus) do
						stats[name] = stats[name] + value
					end
				end
			end
			for name, set in pairs(sets) do
				if set.pieces[item.name] then
					tsets[name] = tsets[name] + 1
					break
				end
			end
		end
	end
	for name, count in pairs(tsets) do
		for i, bonus in pairs(sets[name].bonus) do
			if i <= count then
				for name, value in pairs(bonus) do
					stats[name] = stats[name] + value
				end
			end
		end
	end
	get_secondary_stats(stats)
	return stats
end

-- Actuellement :
local equipment = {
	head = {
		item = "Stormrage Cover",
		enchant = "Animist's Caress",
	},
	shoulder = {
		item = "Forest Wind Shoulderpads",
		enchant = "Inscription of Faith",
	},
	chest = {
		item = "Stormrage Chestguard",
		enchant = "Enchant Chest - Restore Mana Prime",
	},
	legs = {
		item = "Stormrage Legguards",
		enchant = "Animist's Caress",
	},
	waist = {
		item = "Stormrage Belt",
	},
	wrist = {
		item = "Stormrage Bracers",
		enchant = "Enchant Bracer - Mana Regeneration",
	},
	hands = {
		item = "Mitts of the Treemender",
		enchant = "Enchant Gloves - Healing Power",
		socket1= "Lustrous Star of Elune",
		socket2= "Luminous Noble Topaz",
	},
	feet = {
		item = "Forestlord Striders",
		enchant = "Enchant Boots - Spirit",
		socket1= "Sparkling Azure Moonstone",
		socket2= "Sparkling Azure Moonstone",
	},
	mainhand = {
		item = "Serpentcrest Life-staff",
		enchant = "Enchant Weapon - Mighty Spirit",
	},
	back = {
		item = "Cloak of Whispering Shells",
	},
	finger1 = {
		item = "Violet Signet",
	},
	finger2 = {
		item = "Pure Elementium Band",
	},
	trinket1 = {
		item = "Rejuvenating Gem",
	},
	trinket2 = {
		item = "Heavenly Inspiration",
	},
	neck = {
		item = "Sethekk Oracle's Focus",
	}
}

local stat1 = get_equipment_bonuses(equipment)
--~ for name, value in pairs(stat1) do print(name, value) end

equipment.head = {
	item = "Crown of the Forest Lord",
	enchant = "Glyph of Renewal",
}
equipment.shoulder = {
	item = "Mantle of Autumn",
	enchant = "Greater Inscription of Faith",
}
equipment.chest = {
	item = "Raiments of Nature's Breath",
	enchant = "Enchant Chest - Major Spirit",
}
equipment.waist = {
	item = "The Sleeper's Cord",
}
equipment.legs = {
	item = "Earthsoul Britches",
	enchant = "Silver Spellthread",
}
--~ equipment.feet = {
--~ 	item = "Boots of the Glade-Keeper",
--~ 	enchant = "Enchant Boots - Spirit",
--~ }
equipment.wrist = {
	item = "Lucid Dream Bracers",
	enchant = "Enchant Bracer - Restore Mana Prime",
}
--~ equipment.hands = {
--~ 	item = "Natural Mender's Wraps",
--~ 	enchant = "Enchant Gloves - Major Healing",
--~ }

local stat2 = get_equipment_bonuses(equipment)
--~ for name, value in pairs(stat2) do print(name, value) end

--~ for name, value in pairs(stat1) do
--~ 	local delta = stat2[name] - value
--~ 	if delta ~= 0 then
--~ 		print(name, delta)
--~ 	end
--~ end
